package com.liupaik.pennmarketplace;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

public class MyItemsTab extends TabActivity{
	
	private TabHost tabHost;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.tabmyitems);

	    Resources res = getResources(); // Resource object to get Drawables
	    tabHost = getTabHost();  // The activity TabHost
//	    tabHost.setOnTabChangedListener(this);
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, BuyMyGroup.class);
	    // Initialize a TabSpec for each tab and add it to the TabHost
	    
	    TextView btv = new TextView(this);
	    btv.setText("Buying");
	    btv.setTextSize(15);
	    btv.setGravity(Gravity.CENTER);
	    btv.setHeight(40);
	    btv.setTextColor(Color.WHITE);
	    
	    TextView stv = new TextView(this);
	    stv.setText("Selling");
	    stv.setTextSize(15);
	    stv.setGravity(Gravity.CENTER);
	    stv.setHeight(40);
	    stv.setTextColor(Color.WHITE);
	    
	    
	    spec = tabHost.newTabSpec("buying").setIndicator(btv)
	    //        res.getDrawable(R.drawable.books))
	    .setContent(intent);
	    tabHost.addTab(spec);

	    // Do the same for the other tabs

	    intent = new Intent().setClass(this, SellMyGroup.class);
	    spec = tabHost.newTabSpec("selling").setIndicator(stv)
	    //	                      res.getDrawable(R.drawable.furniture))
	    .setContent(intent);
	    tabHost.addTab(spec);


	    
	    TabWidget tw = getTabWidget();
	    for (int i = 0; i < tw.getChildCount(); i++) { 
			            View v = tw.getChildAt(i); 
			            v.setBackgroundDrawable(getResources().getDrawable 
			(R.drawable.tabindicator)); 
          } 

	    
	}
	
//	private void tabStyling() {
//
//		// change tab background color to red
//		for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
//			tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#990000"));
//
//			/**
//			 * set height and width under TabActivity setting width has no
//			 * effect due to fill_parent layout parameter
//			 */
//			//NOTE i cannot get this part work properly.
//			//tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 30;
//			//tabHost.getTabWidget().getChildAt(i).getLayoutParams().width = 30;
//
//			View tempView= tabHost.getTabWidget().getChildAt(i);
//			/**
//			 * I kept all drawables in selector so that the we could get correct
//			 * drawablea applied to tabs as the selector pointed to has both the
//			 * tabs and the bottom tab-bar drawables referenced
//			 */
//			//tempView.setBackgroundDrawable(res.getDrawable(R.drawable.somedrawable));
//
//		}
//
//		// o set different color for current selected tab to blue
//		tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#009900"));
//	}
//	
//	public void onTabChanged(String tabId) {
//	 
//	    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
//	        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(
//	                Color.parseColor("#990000"));
//	    }
//	    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
//	            .setBackgroundColor(Color.parseColor("#009900"));
//	 
//	}


	
	
	/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menuwithsell, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	
    	case R.id.sellmenu:
    		//finish();
    		startActivity(new Intent(this, Sell.class));
    		return true;
    	
		case R.id.preferences:
			startActivity(new Intent(this, Prefs.class));
			return true; 
			
		case R.id.logout:
			//IMPLEMENT LOGOUT HERE
			
			
			Toast.makeText(
					getApplicationContext(),
					"Successfully Logged Out!",
					Toast.LENGTH_SHORT)
					.show();
			Intent myIntent = new Intent(MyItemsTab.this, Intro.class);
			startActivityForResult(myIntent, 0);
			
//			new AlertDialog.Builder(TabMain.this)
//			//.setTitle(Register.this.getResources().getString(R.string.register_alert))  
//			.setMessage("Successfully Logged Out!")  
//			//.setIcon(R.drawable.book)  
//			.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int id) {
//					Intent myIntent = new Intent(TabMain.this, Intro.class);
//					startActivityForResult(myIntent, 0);
//				}
//			})
//			.show();
			
			return true;
    	}
    	return false;
    }*/
}
